package sk.vasicek.drinkmanager.activity;

import java.util.ArrayList;
import java.util.Random;

import sk.vasicek.drinkmanager.R;
import sk.vasicek.drinkmanager.domain.CustomDrink;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class StatsWidgetProvider extends AppWidgetProvider {

	private static final String ACTION_CLICK = "ACTION_CLICK";
	private ArrayList<CustomDrink> customDrinksLoaded;
	private ArrayList<CustomDrink> customDrinks;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

		// Get all ids
		ComponentName thisWidget = new ComponentName(context, StatsWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		for (int widgetId : allWidgetIds) {
			// Create some random data
			int number = (new Random().nextInt(100));

			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.statswidgetlayout);
			Log.w("WidgetExample", String.valueOf(number));
			// Set the text

			// Register an onClickListener
			Intent intent = new Intent(context, StatsWidgetProvider.class);

			intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.tableLayout1, pendingIntent);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
	}
	
//	private void updateCounts() {
//		// Calendar cal = Calendar.getInstance();
//		// cal.clear(Calendar.HOUR_OF_DAY);
//		// cal.clear(Calendar.MINUTE);
//		// cal.clear(Calendar.SECOND);
//		// cal.clear(Calendar.MILLISECOND);
//		// Date date = cal.getTime();
//		Date date = new Date((long) (new Date().getTime() - DrinkStatService.MILLIS_TO_HOURS_DIVIDER * 24l));
//		DrinkHistoryHelper drinkHistoryHelper = new DrinkHistoryHelper();
//		customDrinksLoaded = CustomDrinkHelper.getAllCustomDrinks();
//		drinkTypes.clear();
//		for (CustomDrink customDrink : customDrinksLoaded) {
//			if (customDrink != null) {
//				String drinkName = customDrink.getName();
//				DrinkUI drinkUI = new DrinkUI(drinkName, drinkName + "Button", drinkName + "Price", drinkName + "Label", drinkName + "Count");
//				drinkUI.setCustomDrink(customDrink);
//				drinkTypes.put(drinkUI.getCustomDrink().getId(), drinkUI);
//			}
//		}
//		
//		if (drinkTypes != null) {
//			for (DrinkUI drinkUI : drinkTypes.values()) {
//				Log.d(TAG, "Generating " + drinkUI.getLabelText(this) + " controll");
//
//				final CustomDrink customDrink = drinkUI.getCustomDrink();
//
//				Button drinkButton = drinkUI.getButton();
//				EditText drinkPrice = drinkUI.getPrice();
//				TextView drinkLabel = drinkUI.getLabel();
//				TextView drinkCount = drinkUI.getCount();
//				Integer drinkEntriesCount = drinkHistoryHelper.countDrinkEntryAfterForName(date, customDrink.getName());
//
//				boolean defaultPricesEnabled = prefs.getBoolean("EnablePrice", true);
//				if (!defaultPricesEnabled) {
//					String name = customDrink.getName();
//					if (name == null || "".equals(name.trim())) {
//						name = getResources().getText(R.string.drinkLabelGeneric).toString();
//						customDrink.setName(name);
//					}
////					customDrinkHelper.updateCustomDrinkOrInsert(customDrink.getId(), customDrink);
//				}
//
//				Log.d(TAG, "Updated drinks for " + customDrink.getName() + ":" + drinkEntriesCount);
//				if (customDrink != null && drinkPrice != null)
//					drinkPrice.setText("" + customDrink.getPrice());
//				if (drinkLabel != null)
//					drinkLabel.setText("" + drinkUI.getLabelText(this));
//				if (drinkCount != null)
//					drinkCount.setText("" + drinkEntriesCount);
//			}
//		}
//	}
}
